package com.twotiger.and.activity.project;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Message;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.twotiger.and.Constants;
import com.twotiger.and.TwoTigerApp;
import com.twotiger.and.activity.WebViewPage;
import com.twotiger.and.activity.base.BaseFragment;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.activity.project.debt.DebtInverstInputAmountPage;
import com.twotiger.and.adapter.ProjectAdapter;
import com.twotiger.and.adapter.ProjectDebtAdapter;
import com.twotiger.and.bean.Basebean;
import com.twotiger.and.bean.BroswerUrl;
import com.twotiger.and.bean.DataList;
import com.twotiger.and.bean.DebtProjectdetailData;
import com.twotiger.and.bean.DebtTransProjectDetail;
import com.twotiger.and.bean.Order;
import com.twotiger.and.bean.ProjectDetail;
import com.twotiger.and.bean.ProjectdetailData;
import com.twotiger.and.util.ArithUtils;
import com.twotiger.and.util.LogUtil;
import com.twotiger.and.util.PreferencesUtils;
import com.twotiger.and.util.TimeUtils;
import com.twotiger.and.util.ViewUtils;
import com.view.pulltorefresh.ILoadingLayout;
import com.view.pulltorefresh.PullToRefreshBase;
import com.view.pulltorefresh.PullToRefreshBase.OnLastItemVisibleListener;
import com.view.pulltorefresh.TwoTigerPullToRefreshListView;

/**
 * 我要投资页面
 */
public class Projectpage extends BaseFragment implements OnClickListener {

    protected static final int GETPROJECTSUCCES = 0;
    protected static final int GETPROJECTFAIL = 1;

    protected static final int GETDEBTPROJECTSUCCES = 2;
    protected static final int GETDEBTPROJECTFAIL = 3;

    private static final int GETMOREDATASUCCES = 6;
    private static final int GETMOREDATAFAIL = 7;

    /**项目列表倒计时*/
    private static final int PROJECT_COMMING = 8;

    private static final int GETDETAILSUCCES = 9;
    private static final int GETDETAILFAIL = 10;

    protected static final int OPENPAYMENTSUCCES = 11;
    protected static final int OPENPAYMENTFAIL = 12;

    private static final int GETDEBTDETAILSUCCES = 13;
    private static final int GETDEBTDETAILFAIL = 14;

    ProjectCommingJob projectCommingJob = new ProjectCommingJob();
    private TwoTigerPullToRefreshListView p2p_project_refresh_list;
    private ArrayList<ProjectDetail> projectItems;
    private ArrayList<DebtTransProjectDetail> projectDebtItems;
    private int pNum = 1;
    private int pNumOfDebt = 1;
    private ProjectAdapter projectAdapter; //直投项目的adapter
    private ProjectDebtAdapter projectDebtAdapter; //转让项目的adapter
    private View no_content_rl;
    private ImageView iv_no_content;
    private BaseHandler handler;
    private HashMap<String, String> params;
    private ProjectdetailData pDetail;
    private Order order;
    private View header_view;
    private LinearLayout ll_experience;
    private TextView experience_name_tv;
    private TextView experience_des;
    private TextView experience_term_tv;
    private TextView project_yield_tv;

    private RadioButton project_all_rdio_1, project_all_rdio_2;

    public final static int WITCH_TIGER_DIRECT = 0;
    public final static int WITCH_TIGER_DEBT = 1;
    private int witch_tag;
    private int temp_which_tag;
    private boolean isShowFlag;

    private boolean isShowHeader;
    private ImageView iv_direct;
    private ImageView iv_trans;

    @Override
    protected View loadViewLayout(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.project_list_layout, null);
        header_view = inflater.inflate(R.layout.project_list_layout_header, null); //初始化listview要添加的头布局
        return view;
    }

    @Override
    protected void findViewById(View view) {
        no_content_rl = (View) view.findViewById(R.id.no_content_rl);
        iv_no_content = (ImageView) view.findViewById(R.id.iv_no_content);
        p2p_project_refresh_list = (TwoTigerPullToRefreshListView) view
            .findViewById(R.id.p2p_project_refresh_list);
        initIdOfHeader();

        project_all_rdio_1 = (RadioButton) view.findViewById(R.id.project_all_rdio_1);
        project_all_rdio_2 = (RadioButton) view.findViewById(R.id.project_all_rdio_2);
        iv_direct = (ImageView) view.findViewById(R.id.iv_direct);
        iv_trans = (ImageView) view.findViewById(R.id.iv_trans);

        ILoadingLayout endLabels = p2p_project_refresh_list.getLoadingLayoutProxy(false, true);
        endLabels.setPullLabel("向上拉动可以刷新");// 刚上拉时，显示的提示    
        endLabels.setRefreshingLabel("加载中...");// 刷新时    
        endLabels.setReleaseLabel("松开可以刷新");// 上来达到一定距离时，显示的提示  
    }

    /**
     * 将listview的header给他remove掉
     */
    private void removeHeaderForList() {
        isShowHeader = false;
        ListView lv = p2p_project_refresh_list.getRefreshableView();
        lv.removeHeaderView(header_view);
    }

    /**
     * 给listview增加一个header来显示万元体验金
     * @param projectDetail 
     */
    private void addHeaderForList(final ProjectDetail projectDetail) {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            return;
        }

        isShowHeader = true;

        if (projectDetail == null) {
            return;
        }

        ListView lv = p2p_project_refresh_list.getRefreshableView();
        lv.addHeaderView(header_view);

        experience_name_tv.setText(projectDetail.getName());
        experience_des.setText(projectDetail.getBoutiqueMsg());
        experience_term_tv.setText(projectDetail.getPeriod() + "天");
        project_yield_tv.setText(ArithUtils.formatProject(projectDetail.getYield(), "#.#") + "");

        ll_experience.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            }
        });
    }

    /**
     * 初始化header的布局id
     */
    private void initIdOfHeader() {
        ll_experience = (LinearLayout) header_view.findViewById(R.id.ll_experience);
        experience_name_tv = (TextView) header_view.findViewById(R.id.experience_name_tv);
        experience_des = (TextView) header_view.findViewById(R.id.experience_des);
        experience_term_tv = (TextView) header_view.findViewById(R.id.experience_term_tv);
        project_yield_tv = (TextView) header_view.findViewById(R.id.project_yield_tv);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.sub_title_left_bt:

            break;
        case R.id.project_all_rdio_1:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            if (witch_tag == WITCH_TIGER_DIRECT) {
                return;
            }
            pNum = 1;
            witch_tag = WITCH_TIGER_DIRECT;
            refreshHead(witch_tag);
            break;
        case R.id.project_all_rdio_2:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            if (witch_tag == WITCH_TIGER_DEBT) {
                return;
            }
            pNumOfDebt = 1;
            witch_tag = WITCH_TIGER_DEBT;
            refreshHead(witch_tag);
            break;
        default:
            break;
        }
    }

    /**
     * 刷新头显示,并且做相关请求
     */
    private void refreshHead(int witch) {
        switch (witch) {
        case WITCH_TIGER_DIRECT:
            project_all_rdio_1.setChecked(true);
            project_all_rdio_2.setChecked(false);
            project_all_rdio_1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            project_all_rdio_2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            iv_direct.setVisibility(View.VISIBLE);
            iv_trans.setVisibility(View.GONE);

            no_content_rl.setVisibility(View.GONE);
            p2p_project_refresh_list.setAdapter(projectAdapter);
            refreshdata();

            break;
        case WITCH_TIGER_DEBT:
            project_all_rdio_1.setChecked(false);
            project_all_rdio_2.setChecked(true);
            project_all_rdio_1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            project_all_rdio_2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            iv_direct.setVisibility(View.GONE);
            iv_trans.setVisibility(View.VISIBLE);

            no_content_rl.setVisibility(View.GONE);
            p2p_project_refresh_list.setAdapter(projectDebtAdapter);
            refreshdataOfDebtTransfer();

            break;
        default:
            break;
        }
    }

    @Override
    protected void setListener() {
        handler = new BaseHandler(mactivity) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case OPENPAYMENTSUCCES:
                    Basebean result_open = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result_open.isOk()) {
                        BroswerUrl responseinfo = JSONObject.parseObject(result_open.data,
                            BroswerUrl.class);
                        Intent intent = new Intent(getActivity(), WebViewPage.class);
                        intent.putExtra("title", "开通三方托管账户");
                        intent.putExtra("url", responseinfo.getUrl());
                        intent.putExtra("BUSINESS_TYPE", WebViewPage.TYPE_PAYMENT_REGIST);
                        mactivity.startNewActivity(intent, R.anim.push_left_in,
                            R.anim.push_left_out, false);
                    }
                    break;
                case OPENPAYMENTFAIL:
                    showToast("网络不佳，请重试");
                    break;
                case GETDEBTPROJECTSUCCES: //债权转让
                    Basebean result_debt_pro = JSONObject.parseObject((String) msg.obj,
                        Basebean.class);
                    if (result_debt_pro.isOk()) {
                        p2p_project_refresh_list.getLoadingLayoutProxy().setLastUpdatedLabel(
                            "最后更新：今天 " + TimeUtils.getFormatDate(" HH:mm"));
                        DataList debtDataList = JSONObject.parseObject(result_debt_pro.data,
                            DataList.class);
                        List<DebtTransProjectDetail> projectlist = JSON.parseArray(
                            debtDataList.getList(), DebtTransProjectDetail.class);
                        if (!projectlist.isEmpty()) {
                            if (witch_tag == WITCH_TIGER_DEBT) {
                                if (projectlist.size() < Integer.parseInt(Constants.pageSize)) {
                                    p2p_project_refresh_list.changeMyFooterViewContent(false);
                                } else {
                                    p2p_project_refresh_list.changeMyFooterViewContent(true);
                                }
                            }
                            fillListDataOfDebt(projectlist);
                        } else {
                            if (pNumOfDebt == 1) {
                                no_content_rl.setVisibility(View.VISIBLE);
                                iv_no_content.setImageResource(R.drawable.no_content_trans);
                            } else {
                                p2p_project_refresh_list.changeMyFooterViewContent(false);
                            }
                        }

                    } else {
                        showToast(result_debt_pro.codeDesc);
                    }
                    p2p_project_refresh_list.onRefreshComplete();
                    break;
                case GETDEBTPROJECTFAIL:
                    showToast("网络不佳，请重试");
                    break;
                case GETPROJECTSUCCES: //老虎直投
                    Basebean result = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result.isOk()) {
                        p2p_project_refresh_list.getLoadingLayoutProxy().setLastUpdatedLabel(
                            "最后更新：今天 " + TimeUtils.getFormatDate(" HH:mm"));
                        DataList dataList = JSONObject.parseObject(result.data, DataList.class);
                        mactivity.application.setProjectlist_timestamp(dataList.getTimestamp());
                        List<ProjectDetail> projectlist = JSON.parseArray(dataList.getList(),
                            ProjectDetail.class);
                        if (!projectlist.isEmpty()) {
                            if (witch_tag == WITCH_TIGER_DIRECT) {
                                if (projectlist.size() < Integer.parseInt(Constants.pageSize)) {
                                    p2p_project_refresh_list.changeMyFooterViewContent(false);
                                } else {
                                    p2p_project_refresh_list.changeMyFooterViewContent(true);
                                }
                            }
                            fillListData(projectlist);
                        } else {
                            if (pNum == 1) {
                                no_content_rl.setVisibility(View.VISIBLE);
                                iv_no_content.setImageResource(R.drawable.no_content);
                            } else {
                                p2p_project_refresh_list.changeMyFooterViewContent(false);
                            }
                        }

                    } else {
                        showToast(result.codeDesc);
                    }
                    p2p_project_refresh_list.onRefreshComplete();
                    break;
                case GETPROJECTFAIL:
                    p2p_project_refresh_list.onRefreshComplete();
                    showToast("网络不佳，请重试");
                    break;
                case GETMOREDATASUCCES:
                    p2p_project_refresh_list.onRefreshComplete();
                    break;
                case GETMOREDATAFAIL:
                    break;
                case PROJECT_COMMING: //项目倒计时
                    int s = p2p_project_refresh_list.getRefreshableView().getFirstVisiblePosition();
                    int e = p2p_project_refresh_list.getRefreshableView().getLastVisiblePosition();
                    List<Integer> r = projectAdapter.getComingProjectIndexs(s, e + 1);
                    if (isShowHeader) {
                        r = projectAdapter.getComingProjectIndexs(s - 1, e);
                    }
                    for (int idx : r) {
                        View view = p2p_project_refresh_list.getRefreshableView().getChildAt(
                            idx - s + 1);
                        if (isShowHeader) {
                            view = p2p_project_refresh_list.getRefreshableView().getChildAt(
                                idx - s + 2);
                        }
                        if (view != null) {
                            ProjectDetail project = projectAdapter.getItem(idx);
                            TextView timer_val = (TextView) view.findViewById(R.id.timer_val);
                            if (timer_val != null) {
                                timer_val.setText(project.getTime2Start());
                            }
                            if ("即将开始".equals(project.getTime2Start())) {
                                if (witch_tag == WITCH_TIGER_DIRECT) {
                                    pNum = 1;
                                } else if (witch_tag == WITCH_TIGER_DEBT) {
                                    pNumOfDebt = 1;
                                }
                                refreshHead(witch_tag);
                            }
                        }
                    }
                    break;
                case GETDEBTDETAILSUCCES:
                    Basebean result_detail_debt = JSONObject.parseObject((String) msg.obj,
                        Basebean.class);
                    if (result_detail_debt.isOk()) {
                        DebtProjectdetailData mDebtProjectdetailData = JSON.parseObject(
                            result_detail_debt.data, DebtProjectdetailData.class);
                        if (0 == mDebtProjectdetailData.getInvestMark()) {
                            showToast("当前投资转让是您发起的，不可进行投资");
                            return;
                        }
                        Intent intent = new Intent(mactivity, DebtInverstInputAmountPage.class)
                            .putExtra("ORDER", mDebtProjectdetailData);
                        startnewActivity(intent, R.anim.slide_in_from_bottom, R.anim.silent_anim,
                            false);
                    } else {
                        showToast(result_detail_debt.codeDesc);
                    }
                    break;
                case GETDEBTDETAILFAIL:

                    break;
                case GETDETAILSUCCES:
                    Basebean result_detail_pro = JSONObject.parseObject((String) msg.obj,
                        Basebean.class);
                    if (result_detail_pro.isOk()) {
                        ProjectdetailData ttt = JSON.parseObject(result_detail_pro.data,
                            ProjectdetailData.class);
                        pDetail = ttt;
                        if (pDetail.getAmount() != 0) {
                            order = new Order();
                            order.setProjectid(pDetail.getProjectId());
                            order.setProjectname(pDetail.getName());
                            order.setProject_yield(pDetail.getYield() + pDetail.getAwardYield());
                            order.setRecyle(pDetail.getPeriod());
                            order.setRepaymenttype(pDetail.getRepayTypeName());
                            order.setStartrepytime(pDetail.getEndInterestDate());
                            order.setStartyidtime(pDetail.getStartInterestDate());
                            order.setCaninverst(Double.parseDouble(pDetail.getRemainAmount()));
                            order.setpType(pDetail.getpType());
                            order.setMinInvest(pDetail.getMinInvest());
                            order.setMaxInvest(pDetail.getMaxInvest());

                            order.setType(pDetail.getType());
                            order.setMode(pDetail.getMode());

                            Intent intent = new Intent(mactivity, InverstInputAmountPage.class)
                                .putExtra("ORDER", order);
                            startnewActivity(intent, R.anim.slide_in_from_bottom,
                                R.anim.silent_anim, false);
                        }
                    } else {
                        showToast(result_detail_pro.codeDesc);
                    }

                    break;
                case GETDETAILFAIL:

                    break;
                default:
                    break;
                }
                super.handleMessage(msg);
            }
        };

        p2p_project_refresh_list.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

            @Override
            public void onLastItemVisible() {
                String footerContent = ((TextView) p2p_project_refresh_list
                    .getChildviewOfFooterview()).getText().toString();
                if ("已无更多项目".equals(footerContent)) {
                    return;
                }
                /**
                 * 加载更多数据
                 */
                if (witch_tag == WITCH_TIGER_DIRECT) {
                    pNum++;
                    refreshdata();
                } else if (witch_tag == WITCH_TIGER_DEBT) {
                    pNumOfDebt++;
                    refreshdataOfDebtTransfer();
                }
            }
        });

        p2p_project_refresh_list
            .setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
                @Override
                public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                    /**
                     * 刷新数据
                     */
                    if (witch_tag == WITCH_TIGER_DIRECT) {
                        pNum = 1;
                    } else if (witch_tag == WITCH_TIGER_DEBT) {
                        pNumOfDebt = 1;
                    }
                    refreshHead(witch_tag);
                }

                @Override
                public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                    /**
                     * 加载更多数据
                     */
                    if (witch_tag == WITCH_TIGER_DIRECT) {
                        pNum++;
                    } else if (witch_tag == WITCH_TIGER_DEBT) {
                        pNumOfDebt++;
                    }
                    refreshHead(witch_tag);
                }
            });

        project_all_rdio_1.setOnClickListener(this);
        project_all_rdio_2.setOnClickListener(this);
    }

    /**
     * 填充列表数据（老虎直投）
     * 
     * @param projectlist
     */
    protected void fillListData(List<ProjectDetail> projectlist) {
        p2p_project_refresh_list.setVisibility(View.VISIBLE);
        //请求第一页数据的时候，需要查看第一条数据是否为万元体验金（万元体验金布局跟其他不同，故区别显示）
        if (pNum == 1) {
            String type = projectlist.get(0).getType();
            if (Constants.TYPE_NID_HEIDONG.equals(type)) {
                removeHeaderForList(); //需要先把list头清掉，再添加
                if (witch_tag == WITCH_TIGER_DIRECT) { //保证只有当前tag为直投的时候才添加万元体验金的头
                    addHeaderForList(projectlist.get(0));
                }
                projectlist.remove(0);//将list数据去掉第一条再赋给adapter
                projectAdapter.resetData(projectlist);
            } else {
                removeHeaderForList(); //需要先把list头清掉，再添加
                projectAdapter.resetData(projectlist);
            }
        } else {
            projectAdapter.addData(projectlist);
        }
        //项目倒计时
        projectCommingJob = new ProjectCommingJob();
        projectCommingJob.start();
    }

    /**
     * 填充列表数据（转让）
     * 
     * @param projectlist
     */
    protected void fillListDataOfDebt(List<DebtTransProjectDetail> projectlist) {
        p2p_project_refresh_list.setVisibility(View.VISIBLE);

        if (pNumOfDebt == 1) {
            removeHeaderForList(); //需要先把list头清掉，再添加
            projectDebtAdapter.resetData(projectlist);
        } else {
            projectDebtAdapter.addData(projectlist);
        }
        //项目倒计时
        projectCommingJob = new ProjectCommingJob();
        projectCommingJob.start();
    }

    /**
     * 获取token
     * @return
     */

    public String getToken() {
        return TwoTigerApp.getInstance().getUser().token;
    }

    //请求开通汇付接口
    public void openpayment() {
        params.clear();
        params.put("token", getToken());
        mactivity.getDate(params, Constants.REGISTHF, handler, OPENPAYMENTSUCCES, OPENPAYMENTFAIL,
            true, true, false);
    }

    //请求直投的项目详情
    public void getProjectDetail(String proId, String mode) {
        params.clear();
        params.put("projectId", proId);
        params.put("type", Constants.TYPE_NID_ZHITOU);
        params.put("mode", mode);
        params.put("token", getToken());
        mactivity.getDate(params, Constants.PROJECTINFOQUERY, handler, GETDETAILSUCCES,
            GETDETAILFAIL, true, true, true);
    }

    //请求转让的项目详情
    public void getProjectDetail(String bId) {
        params.clear();
        params.put("bId", bId);
        params.put("token", getToken());
        mactivity.getDate(params, Constants.BONDINFOQUERY, handler, GETDEBTDETAILSUCCES,
            GETDEBTDETAILFAIL, true, true, true);
    }

    /**
     * 刷新老虎直投的数据
     */
    private void refreshdata() {
        params.clear();
        params.put("pageNum", String.valueOf(pNum));
        params.put("pageSize", Constants.pageSize);
        params.put("timestamp", TimeUtils.getTimeStamp());
        params.put("token", getToken());
        mactivity.getDate(params, Constants.PROJECTLISTQUERY, handler, GETPROJECTSUCCES,
            GETPROJECTFAIL, false, true, false);
    }

    /**
     * 刷新转让计划的数据
     */
    private void refreshdataOfDebtTransfer() {
        params.clear();
        params.put("pageNum", String.valueOf(pNumOfDebt));
        params.put("pageSize", Constants.pageSize);
        params.put("timestamp", TimeUtils.getTimeStamp());
        params.put("token", getToken());
        mactivity.getDate(params, Constants.BONDLISTQUERY, handler, GETDEBTPROJECTSUCCES,
            GETDEBTPROJECTFAIL, false, true, false);
    }


    @Override
    protected void processLogic() {
        projectItems = new ArrayList<ProjectDetail>();
        projectDebtItems = new ArrayList<DebtTransProjectDetail>();
        params = mactivity.getParm(); //初始化请求参数

        p2p_project_refresh_list.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

        projectAdapter = new ProjectAdapter(mactivity, projectItems, this); //初始化老虎直投的adapter
        projectDebtAdapter = new ProjectDebtAdapter(mactivity, projectDebtItems, this); //初始化债权转让的adapter

        refreshHead(witch_tag);
    }

    @Override
    public boolean isNeedLogin() {
        return false;
    }

    @Override
    public void rebuildTitleStyle() {
        subTitle.setBgColor(Color.WHITE).setTitle(getTitle()).setLeftName("").setRightName("")
            .setColor(Color.BLACK).hideBtns().hideBottomLine();
        mactivity.tintManager.setTintResource(R.drawable.bg_common_statusbar);
        mactivity.setLayoutPadding(false);
    }

    @Override
    public String getTitle() {
        return "项目";
    }

    @Override
    public void reLoadData() {

    }

    @Override
    public void onPause() {
        super.onPause();
        projectCommingJob.stop();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) { //从显示切换到不显示
            projectCommingJob.stop();
            isShowFlag = false;
            temp_which_tag = witch_tag;
        } else { //从不显示切换到显示
            projectCommingJob = new ProjectCommingJob();
            projectCommingJob.start();

            isShowFlag = true;
            witch_tag = WITCH_TIGER_DIRECT;
            pNum = 1;
            pNumOfDebt = 1;
            refreshHead(witch_tag);
        }
    }

    @Override
    public void onResume() {
        if (isShowFlag) {
            witch_tag = temp_which_tag;
            refreshHead(witch_tag);
        }
        if (WITCH_TIGER_DEBT == mactivity.application.getWitchTag()) {
            witch_tag = WITCH_TIGER_DEBT;
            refreshHead(witch_tag);
            mactivity.application.setWitchTag(-1);
        }
        super.onResume();
    }

    /**
     * 项目倒计时job
     * @Description:
     * 生命周期,start--->stop必须销毁 
     * @Author douxy
     * @Date 2015年8月18日 下午12:57:25
     */
    class ProjectCommingJob implements Runnable {

        /**每个县城只能start一次*/
        private boolean isRun = false;
        private boolean stop = false;

        public void stop() {
            this.stop = true;
        }

        public void start() {
            if (isRun)
                return;
            isRun = true;
            stop = false;
            handler.post(this);
        }

        @Override
        public void run() {
            int s = p2p_project_refresh_list.getRefreshableView().getFirstVisiblePosition();
            int e = p2p_project_refresh_list.getRefreshableView().getLastVisiblePosition();
            List<Integer> r = projectAdapter.getComingProjectIndexs(s, e);
            if (isShowHeader) {
                r = projectAdapter.getComingProjectIndexs(s - 1, e - 1);
            }
            LogUtil.info(ProjectCommingJob.class, "project: " + " fIdx:" + s + " eIdx:" + e + " r "
                                                  + r.toArray());
            if (r.size() > 0) {
                handler.sendEmptyMessage(PROJECT_COMMING);
            }
            if (projectAdapter.getComingProjectIndexs(-1, -1).size() > 0 && !stop) {
                handler.postDelayed(this, 1000);
            }
        }
    }
}
